/**
 *******************************************************************************
 * @file uart_driver.h
 * @author .ISS_AUTO (hudandan@issauto.com)
 * @brief This file is interfaces for uart driver.
 *        MCU: N32G45x
 *
 * @date 2023-06-08    1. Uart初始配置函数接口：
 *                        void UartX_Configuration(uint8_t chan, uint32_t baud_rate);
 *                     2. Uart中断状态标志位检查接口：
 *                        int Check_USARTx_TXC_IntStatus(uint8_t chan);
 *                     3. Uart发送接口：
 *                        void USARTx_SendData(uint8_t chan, uint16_t dat);
 *                     4. Uart接收接口：
 *                        uint16_t USARTx_ReceiveData(uint8_t chan);
 *       2024-02-26    1. Uart阻塞发送接口：
 *                        void USARTx_SendData_Blocking(uint8_t chan, uint16_t dat);
 *       2024-06-20    1. 优化Uart初始配置函数接口，
 *                        分为 “Uart(外设IO)初始配置接口” 与 “Uart中断配置接口”；
 *       2024-07-15    1. 新增当前通道波特率读取，
 *                        USARTx_GetBaudRate()；
 *       2025-01-03    1. 优化UART发送管理，并更新阻塞式和非阻塞式发送接口；
 *
 * @copyright Copyright (c) 2023 ISSAUTO TECH Co., Ltd. All rights reserved.
 *
 *******************************************************************************
 */
/* Define to prevent recursive inclusion -------------------------------------*/

#ifndef __UART_DRIVER_H
#define __UART_DRIVER_H

#ifdef __cplusplus
extern "C" {
#endif

/* Exported Includes ---------------------------------------------------------*/
#include "main.h"

/* Exported types ------------------------------------------------------------*/
typedef enum UART_CHANNEL_Tag {
    UART_CHANNEL_1 = 0, //!< UART channel 1
    UART_CHANNEL_2,
    UART_CHANNEL_3,
    UART_CHANNEL_4,
    UART_CHANNEL_5,
    UART_CHANNEL_6,
    UART_CHANNEL_7,

    UART_CHANNEL_MAX
} UART_CHANNEL_E;

typedef enum {
    UART_STATE_RESET             = 0x00U,    /*!< Peripheral is not yet Initialized
                                                   Value is allowed for gState and RxState */
    UART_STATE_READY             = 0x20U,    /*!< Peripheral Initialized and ready for use
                                                   Value is allowed for gState and RxState */
    UART_STATE_BUSY              = 0x24U,    /*!< an internal process is ongoing
                                                   Value is allowed for gState only */
    UART_STATE_BUSY_TX           = 0x21U,    /*!< Data Transmission process is ongoing
                                                   Value is allowed for gState only */
    UART_STATE_TIMEOUT           = 0xA0U,    /*!< Timeout state
                                                   Value is allowed for gState only */
    UART_STATE_ERROR             = 0xE0U     /*!< Error
                                                   Value is allowed for gState only */
} UART_StateTypeDef;

typedef struct __UART_DriverTypeDef {
    // USART_Module *USARTx;

    uint32_t BaudRate; /*!< This member configures the USART communication baud rate.
                            The baud rate is computed using the following formula:
                             - IntegerDivider = ((PCLKx) / (16 * (USART_InitStruct->BaudRate)))
                             - FractionalDivider = ((IntegerDivider - ((u32) IntegerDivider)) * 16) + 0.5 */

    UART_StateTypeDef gState;    /*!< UART state information related to Tx operations. */

} UART_DriverTypeDef;

/* Exported constants --------------------------------------------------------*/

/* Exported macro ------------------------------------------------------------*/
/*!< Printf channel setting */
#if !defined(UARTx_PRINTF)
#define UARTx_PRINTF            UART_CHANNEL_6 //!< Default -> UART6
#endif

/* Exported variables --------------------------------------------------------*/
extern const USART_Module *pUSARTx_MAP[];
extern const IRQn_Type USARTx_IRQn[];
extern volatile UART_DriverTypeDef uartx_driver[UART_CHANNEL_MAX];

/* Exported functions --------------------------------------------------------*/

#if (UART_1_CFG_ENABLE)
extern void Uart1_Configuration(uint32_t baud_rate);
#endif
#if (UART_2_CFG_ENABLE)
extern void Uart2_Configuration(uint32_t baud_rate);
#endif
#if (UART_3_CFG_ENABLE)
extern void Uart3_Configuration(uint32_t baud_rate);
#endif
#if (UART_5_CFG_ENABLE)
extern void Uart5_Configuration(uint32_t baud_rate);
#endif
#if (UART_6_CFG_ENABLE)
extern void Uart6_Configuration(uint32_t baud_rate);
#endif

/**
 * @brief Specified uart channel Configuration
 *
 * @param[in] chan specified uart channel
 * @param[in] baud_rate
 * @return
 */
extern void UartX_Configuration(uint8_t chan, uint32_t baud_rate);

/**
 * @brief Specified uart channel Interrupt Configuration
 *
 * @param[in] chan specified uart channel
 * @param[in] r_cmd - configure "Receive Data register not empty interrupt"
 * @param[in] priority
 * @return
 */
extern void UartX_ConfigurationInt(uint8_t chan, FunctionalState r_cmd, uint8_t priority);

/**
 * @brief return the USART Baud Rate
 *
 * @param chan specified uart channel
 * @return baudRate
 */
extern uint32_t USARTx_GetBaudRate(uint8_t chan);

/**
 * @brief Configure the USART Baud Rate
 *
 * @param chan specified uart channel
 * @param baudRate
 */
extern void USARTx_SetBaudRate(uint8_t chan, uint32_t baudRate);

/**
 * @brief  Sends an amount of data in blocking mode.
 *
 * @param[in] chan specified uart channel
 * @param[in] pDat the data to transmit
 * @param[in] pDatLen Amount of data elements (u8) to be sent
 * @return uint16_t
 */
extern uint16_t USARTx_SendData_Blocking(uint8_t chan, const uint8_t *pDat, uint16_t pDatLen);

/**
 * @brief  Transmits single data through the USARTx peripheral.
 *
 * @param[in] chan specified uart channel
 * @param[in] dat the data to transmit.
 */
extern void USARTx_SendData(uint8_t chan, uint16_t dat);

/**
 * @brief Returns the most recent received data by the USARTx peripheral.
 *
 * @param[in] chan specified uart channel
 * @return none
 */
extern uint16_t USARTx_ReceiveData(uint8_t chan);


#ifdef __cplusplus
}
#endif

#endif /* __UART_DRIVER_H */

/* END OF FILE ---------------------------------------------------------------*/
